package com.care.doctor.server.service.impl;

import com.alibaba.fastjson.JSON;
import com.care.common.core.domain.Result;
import com.care.doctor.common.domain.DTOLabel;
import com.care.doctor.server.mapper.LabelMapper;
import com.care.doctor.server.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 靳家振
 * @Date: 2024/3/26 09:33
 * @Description:
 **/

@Service
public class LabelServiceImpl implements LabelService {
    @Autowired
    private LabelMapper mapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public Result listAll() {

        if (Boolean.TRUE.equals (redisTemplate.hasKey ("labelList"))) {
            ArrayList<DTOLabel> dtoLabels = new ArrayList<> ();
            List<String> list = redisTemplate.opsForList ().range ("labelList", 0, -1);
            for (String s : list) {
                DTOLabel dtoLabel = JSON.parseObject (s, DTOLabel.class);
                dtoLabels.add (dtoLabel);
            }
            return Result.success (dtoLabels);
        }
        List<DTOLabel> labels = mapper.listAll ();
        for (DTOLabel label : labels) {
            redisTemplate.opsForList ().rightPush ("labelList",JSON.toJSONString (label));
        }
        return Result.success (labels);
    }

    @Override
    public Result listById(Integer departmentId) {
        List<DTOLabel> labels = mapper.listById (departmentId);
        return Result.success (labels);
    }

    @Override
    public Result findByLabelId(Integer labelId) {
        return mapper.findByLabelId(labelId);
    }
}
